package com.jacen.fzxs.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jacen.fzxs.entity.web.AppEntity;
import com.jacen.fzxs.entity.webservice.Response;
import com.jacen.fzxs.entity.webservice.ResponseState;
import com.jacen.fzxs.exception.WebException;
import com.jacen.fzxs.util.HttpUtil;
import com.jacen.fzxs.util.WebServiceUtil;
/**
 * app接入controller
 * @author Jacen
 *
 */
@Controller
public class AppController {
	
	@RequestMapping(value = "/app.do", method = RequestMethod.POST)
	@ResponseBody
	public String app(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String requestXml = HttpUtil.readFromRequest(request);
		String responseXml = "";
		try{
			responseXml = WebServiceUtil.handler(requestXml, request.getSession().getServletContext(), request);
		}catch(WebException e){
			Response<AppEntity> appResponse = new Response<AppEntity>();
			appResponse.setStateCode(e.getExceptionCode());
			ResponseState responseState = appResponse.getResponseState();
			responseState.addError(e.getMessage());
			responseXml = WebServiceUtil.formatResponse(appResponse, null).asXML();
		}
		return responseXml;
	}

}
